// pages/dingdan/dingdan.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dingdannav: [{ id: '0', tit: '全部' }, { id: '1', tit: '待付款' }, { id: '2', tit: '待发货' }, { id: '3', tit: '待收货' }, { id: '4', tit: '待评价' }],
    current:0,
    order:[],
    // pstype:['堂食','外卖','自取']
    stats: ['待付款', '已付款', '已发货', '订单完成', '退款完成订单关闭', '退款中', '订单关闭', '订单取消', '退款中', '待领取']
  
  },
  navtap:function(e){
    this.setData({
      current:e.currentTarget.id
    })
  },
  /*取消订单 */
  refund:function(e){
    var that = this;
    wx.request({
      url: app.globalData.directory + 'api/v2/order/close',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      method:"POST",
      data:{
        id: e.currentTarget.dataset.sn
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          if(res.data.msg=='success'){
            wx.showToast({
              title: '取消订单成功',
              icon: 'successs',
              duration: 2000,
            })
            that.demand('1');
          }else{
            wx.showToast({
              title: res.data.msg ,
              icon: 'none',
              duration: 2000,
            })
          }
        } else {
          wx.showToast({
            title: '请求失败,请重试',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  /*付款 */
  buy:function(e){
    var that=this;
    wx.request({
      url: app.globalData.directory + 'api/v2/pay/order_sn/' + e.currentTarget.dataset.sn,
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          wx.requestPayment({
            'timeStamp': res.data.data.timestamp,
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
            'success': function (res) {
            },
            'fail': function (res) {
              wx.showToast({
                title:res.errMsg,
                icon: 'none',
                duration: 2000,
              })
            }
          })
        } else {
          wx.showToast({
            title: '请求失败,请重试',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  conswiper:function(e){
    this.setData({
      current: e.detail.current
    })
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    this.demand(e.detail.current);
  },
  /*再来一单 */
  order1:function(){
    wx.switchTab({
      url: '../order/order'
    })
  },
  /*评价 */
  pj: function (e) {
    wx.navigateTo({
      url: '../to-evaluate/to-evaluate?id='+e.currentTarget.dataset.id,
    })
  },
  demand:function(id){
    var that = this;
    wx.request({
      url: app.globalData.directory + 'api/v2/order/getlist/' + id,
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          console.log(res.data.data,'lihuining')
          wx.hideLoading();
          that.setData({
            order: res.data.data,
          })
        } else {
          wx.hideLoading();
          wx.showToast({
            title: '请求失败,请重试',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  //确认收货
  bSure: function (e) {
    console.log(e)
    var sn2 = e.currentTarget.dataset.sn
    console.log(sn2)
    var that = this
    wx.request({
      url: app.globalData.directory + 'api/v2/order/confirmOrder',
      data: { sn: sn2 },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == "ok") {
          wx.showToast({
            title: "处理订单成功",
            icon: 'success',
            duration: 2000,
          })
          setTimeout(function () {
            wx.redirectTo({
              url: '../dingdan/dingdan?id=4',
            })
          }, 1000)
        } else {
          wx.showToast({
            title: "处理失败",
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    this.setData({
      current:options.id,
    })
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    that.demand(options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})